package cn.seaboot;

import cn.seaboot.commons.core.FastJsonUtils;
import cn.seaboot.commons.file.FileUtils;
import cn.seaboot.flake.entity.MappingTable;
import cn.seaboot.flake.entity.NormTable;
import cn.seaboot.flake.reverse.CreateTableParser;
import cn.seaboot.flake.reverse.NormToMappingConverter;
import net.sf.jsqlparser.JSQLParserException;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * 从建表语句中获取表结构参数
 */
public class GenerateCode {

    public static void main(String[] args) throws JSQLParserException, IOException {
        CreateTableParser parser = new CreateTableParser();
        NormToMappingConverter converter = new NormToMappingConverter();

        String sql = "CREATE TABLE `t_sys_dictionary_config` (\n" +
                "  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n" +
                "  `schema_name` varchar(64) DEFAULT NULL COMMENT '库名',\n" +
                "  `table_name` varchar(64) DEFAULT NULL COMMENT '表名',\n" +
                "  `table_alias` varchar(32) DEFAULT NULL COMMENT '表别名',\n" +
                "  `column_name` varchar(64) DEFAULT NULL COMMENT '列名',\n" +
                "  `column_alias` varchar(32) DEFAULT NULL COMMENT '列别名',\n" +
                "  `default_style` varchar(32) DEFAULT NULL COMMENT '默认样式',\n" +
                "  `editable` tinyint(1) unsigned DEFAULT '0' COMMENT '是否可编辑',\n" +
                "  PRIMARY KEY (`id`)\n" +
                ") ENGINE=InnoDB AUTO_INCREMENT=111112 DEFAULT CHARSET=utf8mb4 COMMENT='数据字典配置';";

        NormTable normTable = parser.parse(sql);
        MappingTable mappingTable = converter.normTableToMappingTable(normTable);

        String json = FastJsonUtils.toJSONString(mappingTable);

        File out = new File("D:\\reverse\\" + normTable.getTableName() + ".json");
        FileUtils.writeString(out, json, Charset.defaultCharset());

        System.out.println(json);
    }

}